package com.example.tourapp.ui;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.tourapp.Pojo.Info;
import com.example.tourapp.R;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Content_updata extends AppCompatActivity {
    //组件
    private ImageView comeBack;
    private TextView time_t;
    private EditText ed_content;
    private ImageView del;
    private ImageView add;
    //全局
    private int id=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_updata);
        initView();
        inifunction();
    }


    private void initView() {
        comeBack = (ImageView) findViewById(R.id.comeBack);
        time_t = (TextView) findViewById(R.id.update_time);
        ed_content = (EditText) findViewById(R.id.update_content);
        del = (ImageView) findViewById(R.id.del);
        add = (ImageView) findViewById(R.id.add);

        //Intent获取数据
        Intent intent = getIntent();
        String content = intent.getStringExtra("content");
        String date = intent.getStringExtra("date");
       id = intent.getIntExtra("id",-1);

        ed_content.setText(content);
        time_t.setText(date);
    }

    private void inifunction() {
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = ed_content.getText().toString().trim();
                if (TextUtils.isEmpty(text)) {
                    Toast.makeText(Content_updata.this, "请输入要添加的内容", Toast.LENGTH_SHORT).show();
                    return;
                }

                SimpleDateFormat format = new SimpleDateFormat("YYYY年MM月dd HH:mm:ss");
                Date date = new Date();
                Info info = new Info(text, format.format(date));
                //update 需要在实体类中创建一个空参构造
                info.update(id);
                Toast.makeText(Content_updata.this, "修改成功!", Toast.LENGTH_SHORT).show();
                finish();

            }
        });
        //删除
        del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ed_content.setText("");
            }
        });

        //返回
        comeBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
}
